package 单周赛;

import java.util.Arrays;

/**
 * @
 * @date 2024/12/08
 */
public class 第427场单周赛 {

    public static void main(String[] args) {

        第427场单周赛 impl = new 第427场单周赛();
//        System.out.println(impl.maxRectangleArea(new int[][]{{100, 80}, {67, 79}, {100, 79}, {67, 80}, {80, 47}}));
        System.out.println(impl.maxRectangleArea(new int[][]{{1, 1}, {1, 3}, {3, 1}, {3, 3}, {1, 2}, {3, 2}}));

    }

    /**
     * 模拟
     */
    public int[] constructTransformedArray(int[] nums) {

        int[] result = new int[nums.length];

        for (int i = 0; i < nums.length; i++) {

            if (nums[i] > 0) {
                result[i] = nums[(i + nums[i]) % nums.length];
            } else if (nums[i] == 0) {
                result[i] = nums[i];
            } else {
                int j = i;
                for (int i1 = 0; i1 < Math.abs(nums[i]); i1++) {
                    j--;
                    if (j == -1) {
                        j = nums.length - 1;
                    }
                }
                result[i] = nums[j];
            }
        }

        return result;
    }

    /**
     * 暴力模拟
     */
    public int maxRectangleArea(int[][] points) {
        int ans = -1;
        for (int i = 0; i < points.length; i++) {
            for (int j = i + 1; j < points.length; j++) {
                for (int k = j + 1; k < points.length; k++) {
                    for (int l = k + 1; l < points.length; l++) {

                        int[][] arr = {points[i], points[j], points[k], points[l]};
                        Arrays.sort(arr, (a, b) -> a[0] - b[0]);

                        if (arr[0][0] == arr[1][0] &&
                                arr[2][0] == arr[3][0]) {
                            int x1 = arr[0][0], x2 = arr[2][0];

                            Arrays.sort(arr, (a, b) -> a[1] - b[1]);
                            if (arr[0][1] == arr[1][1] &&
                                    arr[2][1] == arr[3][1]) {
                                int y1 = arr[0][1], y2 = arr[2][1];

                                if (check(points, x1, x2, y1, y2)) {
                                    continue;
                                }
                                ans = Math.max(ans, (y2 - y1) * (x2 - x1));
                            }
                        }
                    }
                }
            }

        }

        return ans;
    }

    private boolean check(int[][] points, int x1, int x2, int y1, int y2) {
        for (int[] point : points) {
            if ((point[0] == x1 || point[0] == x2) &&
                    (point[1] == y1 || point[1] == y2)) {
                continue;
            }
            if (point[0] >= x1 && point[0] <= x2 &&
                    point[1] >= y1 && point[1] <= y2) {
                return true;
            }
        }
        return false;
    }

    /**
     * 前缀和+动态规划
     */
    public long maxSubarraySum(int[] nums, int k) {
        long ans = Long.MIN_VALUE;
        long[] prefixSum = new long[nums.length + 1];
        for (int i = 0; i < nums.length; i++) {
            prefixSum[i + 1] = prefixSum[i] + nums[i];
        }
        long[] dp = new long[nums.length];

        for (int i = 0; i < nums.length; i++) {
            if (i + 1 < k) {
                continue;
            }
            dp[i] = prefixSum[i + 1] - prefixSum[i + 1 - k];

            int preI = i + 1 - k - 1;
            if (preI >= 0) {
                dp[i] = Math.max(dp[i], dp[i] + dp[preI]);
            }
            ans = Math.max(ans, dp[i]);
        }

        return ans;
    }

}
